<template>
	<view>
		<!-- <u-navbar back-icon-color="#000000" :back-icon-size="40" :background="{color: bgColor}" :border-bottom="false" :title-bold="true"> -->
		<u-navbar :is-back="false" :background="{backgroundColor: bgColor}" :border-bottom="false" :title-bold="true">
			<view class="navbar-left" @click="goBack">
				<u-icon name="arrow-left" color="#000000" size="38"></u-icon>
			</view>
			<view v-if="isFenlei" class="slot-wrap" style="text-align: center;box-sizing: border-box;padding-right: 66rpx;">
					{{itemTitle}}
					<!-- params.itemName = params.itemName.replace(/(^\s*)|(\s*$)/g,""); -->
			</view>
			<view class="slot-wrap" v-if="!isFenlei">
				<view class="search-wrap">
					<u-input :type="'text'" :focus="true" v-model="params.itemName" :clearable="true" placeholder="葡萄酒" placeholder-style="color:#CCCCCC;font-size:28rpx;" :confirm-type="'搜索'"  @input="params.itemName = params.itemName.replace(/\s+/g,'')" @confirm="handleSearch"/>
					<!-- params.itemName = params.itemName.replace(/(^\s*)|(\s*$)/g,""); -->
				</view>
			</view>
			<view class="navbar-right" slot="right" @click="handleSearch" v-if="!isFenlei">
				<view class="message-box right-item">
					<u-icon name="search" color="#000000" size="38"></u-icon>
				</view>
			</view> 
		</u-navbar>
		<view v-if="!isList" class="u-p-l-35 u-p-r-35">
			<view class="u-flex u-row-between">
				<text class="u-font-32 text-bold" style="line-height: 80rpx;">历史搜索</text>
				<u-icon name="trash" :size="40" :color="infoColor" @click="clearHistory"></u-icon>
			</view>
			<view class="history">
				<view class="history-box" v-for="(item, index) in list" :key="index">
					<text class="u-font-28" style="white-space: nowrap;" @click="params.itemName = item;handleSearch()">{{ item }}</text>
				</view>
				<!-- <view v-if="!status" class="shouqi" @click="changeList(1)">
					<u-icon name="arrow-down" color="#CCCCCC" size="20"></u-icon>
				</view>
				<view v-else class="shouqi" @click="changeList(0)">
					<u-icon name="arrow-up" color="#CCCCCC" size="20"></u-icon>
				</view> -->
			</view>
		</view>
		<view v-else class="u-p-l-35 u-p-r-35 u-p-t-20">
			<!-- 列表 -->
			<!-- <lazy-list ref="childWaterfall" :flowList="flowList" :loadStatus="loadStatus" v-if="flowList.length!=0"/> -->
		<!-- 	<u-waterfall v-if="flowList.length!=0" v-model="flowList" ref="uWaterfall" style="margin-top: 20rpx;">
				<template v-slot:left="{leftList}">
					<lazy-item :InitData="leftList"></lazy-item>
				</template>
				<template v-slot:right="{rightList}">
					<lazy-item :InitData="rightList"></lazy-item>
				</template>
			</u-waterfall> -->
			<u-waterfall v-if="flowList.length!=0" v-model="flowList" ref="uWaterfall">
				<template v-slot:left="{leftList}">
					<lazy-item :InitData="leftList"></lazy-item>
				</template>
				<template v-slot:right="{rightList}">
					<lazy-item :InitData="rightList"></lazy-item>
				</template>
			</u-waterfall>
			<u-empty text="没有相关的产品" mode="list" v-else style="margin-top: 200rpx;"></u-empty>
		</view>
		
	</view>
</template>

<script>
	import SnWaterfall from '@/components/sn-waterfall.vue';
	import LazyList from './lazyList.vue'
	// import LazyItem from './lazyItem.vue';
	import LazyItem from './ItemList.vue';
	import { useShopStore } from '@/store/shop.js';
	import { mapState } from 'pinia';
	export default {
		components: { LazyList,LazyItem,SnWaterfall },
		data() {
			return {
				isFenlei: false,
				itemTitle:'',
				status: 0,
				isList: false,
				flowList: [],
				loadStatus: 'loadmore',
				params: {
					itemName: '',
					pageIndex: 1,
					PageSize: 10
				},
				list: uni.getStorageSync('search_history') || [],
				historyList: uni.getStorageSync('search_history').slice(0, 5),
				moreList: uni.getStorageSync('search_history')
			}
		},
		computed:{
			...mapState(useShopStore, {storeShop: 'shop'})
		},
		onLoad(option) {
			if (this.storeShop.id) {
				this.params.ShopId = this.storeShop.id
			}
			if (option.itemId) {
				this.params.itemId = option.itemId
				this.itemTitle = option.itemName
				this.isFenlei = true
				this.isList = true
				this.getProductList()
			}
		},
		async onReachBottom() {
			if (this.loadStatus != 'nomore') {
				await this.getProductList();
			}
		},
		methods: {
			keypressFn(){
					console.log(222);
			},
			goBack() {
				if (this.isFenlei) uni.navigateBack()
				if (this.params.itemName == '') {
					this.$mRouter.switchTab({ route: '/pages/index/index' })
				} else {
					this.$mRouter.redirectTo({ route: '/pages/index/search' })
				}
				// this.params.itemName = ''
				// if (this.isList) {
				// 	this.isList = false
				// } else {
				// 	uni.navigateBack();
				// }
			},
			searchInputFn(){
				this.params.itemName = this.params.itemName.replace(/\s+/g,'');
				console.log(this.params.itemName)
			},
			changeList(status) {
				this.status = status
				if (status == 0) {
					this.list = this.historyList
				} else {
					this.list = this.moreList
				}
			},
			handleSearch() {
				if(this.params.itemName === ''){
					this.params.itemName = '葡萄酒'
				}
				this.setHistory()
				this.isList = true
				this.params.pageIndex = 1
				this.flowList = []
				this.$nextTick(() => {
					this.$refs.uWaterfall.clear();
				})
				
				this.getProductList()
			},
			// 获取商品数据
			async getProductList() {
				this.loadStatus = 'loading'
				if(this.params.itemName === ''){
					delete this.params.itemName
				}
				console.log(this.params);
				const { list } = await this.$http.get('api/v1.2/ShanNiang/GetItemsPage', { params: this.params } );
				if(list == null){
					this.loadStatus = 'nomore'
					return;
				};
				if (list.length == 0) {
					this.loadStatus = 'nomore'
					return;
				}
				this.flowList = this.flowList.concat(list)
				this.loadStatus = 'loadmore'
				this.params.pageIndex++;
			},
			// 存储搜索记录
			setHistory() {
				if (!this.params.itemName) return;
				let arr = uni.getStorageSync('search_history') || []
				// 判断搜索词 是否在历史记录中 如果存在 删除之前的
				const index = arr.findIndex(item => {
					return item == this.params.itemName
				})
				if (index != -1) arr.splice(index, 1)
				arr.unshift(this.params.itemName)
				// 如果数组大于20个 则删除第一个
				if (arr.length > 20) arr.shift()
				uni.setStorageSync('search_history', arr)
			},
			// 清除搜索记录
			clearHistory() {
				uni.showModal({
					// title: '提示',
					content: '确认删除全部历史记录？',
					success: (res) => {
						if (res.confirm) {
							uni.removeStorageSync('search_history')
							this.list = []
						}
					}
				});
				
			}
		}
	}
</script>

<style lang="scss" scoped>
	.slot-wrap {
		// display: flex;
		// align-items: center;
		// flex: 1;
		width: 100%;
	}
	.search-wrap {
		margin: 0 20rpx;
		flex: 1;
		border: 1rpx solid $theme-color;
		padding: 0 30rpx;
		height: 71rpx;
		border-radius: 35rpx;
	}
	.navbar-right {
		margin-right: 35rpx;
	}
	.navbar-left {
		margin-left: 35rpx;
	}
	.search-input {
		border: 1px solid $theme-color;
		border-radius: 35rpx;
		padding-left: 20rpx;
		height: 70rpx;
	}
	.history {
		width: 90%;
		display: flex;
		flex-wrap: wrap;
		.history-box {
			background-color: #FFFFFF;
			border: 1rpx solid #CCCCCC;
			border-radius: 50rpx;
			margin: 10rpx 20rpx 10rpx 0;
			padding: 10rpx 20rpx;
		}
		.shouqi {
			width: 50rpx;
			height: 50rpx;
			border-radius: 50%;
			border: 1rpx solid #CCCCCC;
			background-color: #FFFFFF;
			text-align: center;
			margin-top: 17rpx;
		}
	}
	
</style>
